//
//  ESUNRefreshFooterView.m
//  TEST
//
//  Created by wang yongfeng on 14-3-22.
//  Copyright (c) 2014年 wang yongfeng. All rights reserved.
//

#import "ESUNRefreshFooterView.h"

@implementation ESUNRefreshFooterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    //Drawing code
    [self adjustFrame];
}
*/

- (void)setScrollView:(UIScrollView *)scrollView
{
    // 移除之前的监听器
    [_scrollView removeObserver:self forKeyPath:dESRefreshContentOffset context:nil];
    // 监听contentOffset
    
    [scrollView addObserver:self forKeyPath:dESRefreshContentOffset options:NSKeyValueObservingOptionNew context:nil];
    // 设置scrollView
    _scrollView = scrollView;
    [self adjustFrame];
    [_scrollView addSubview:self];
}


//进行刷新
- (void)beginRefreshing
{
    if(![self isRefreshing])
    {
        [self setRefreshState:eESRefreshStateRefresh];
    }
}

//结束刷新
- (void)endRefreshing
{
    [self setRefreshState:eESRefreshStateNormal];
    [self adjustFrame];
}

//设置刷新状态
- (void)setRefreshState:(ESUNRefreshState)state
{
    if(_state == state)
    {
        return;
    }
    //根据状态执行不同的操作
    switch (state) {
        case eESRefreshStateNormal:
        {
            //默认状态
            //通知代理
            [self.stateLable setText:dESRefreshFooterNormal];
            if ([self.delegate respondsToSelector:@selector(refreshViewEndRefresh:)]) {
                [self.delegate refreshViewEndRefresh:self];
            }
            break;
        }
        case eESRefreshStatePull:
        {
            //松开刷新
            [self.stateLable setText:dESRefreshFooterPull];
            break;
        }
        case eESRefreshStateRefresh:
        {
            //正在刷新
            //通知代理
            [self.stateLable setText:dESRefreshFooterRefresh];
            if ([self.delegate respondsToSelector:@selector(refreshViewBeginRefresh:)])
            {
                [self.delegate refreshViewBeginRefresh:self];
            }
            break;
        }
        default:
            break;
    }
    [super setRefreshState:state];
}

- (void)adjustFrame
{
    float maxY = MAX(self.scrollView.contentSize.height, self.scrollView.frame.size.height);
    [self setFrame:CGRectMake(0, maxY, self.scrollView.frame.size.width, dESRefreshViewHeight)];
}

//监听scrollView的contentOffSet的变化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (![dESRefreshContentOffset isEqualToString:keyPath]) return;
    [self.indicator startAnimating];
    CGFloat offsetY = self.scrollView.contentOffset.y; //偏移量
    float maxY = MAX(self.scrollView.contentSize.height, self.scrollView.frame.size.height);
    if(self.canRefresh == NO)
    {
         [self.stateLable setText:@"没有更多啦"];
        return;
    }
    if(self.scrollView.isDragging)
    {
        CGFloat validOffsetY = maxY - self.scrollView.frame.size.height + dESRefreshViewHeight;
        if(_state == eESRefreshStatePull && offsetY < validOffsetY)
        {
            //转为普通状态
            [self setRefreshState:eESRefreshStateNormal];
            [self.stateLable setText:dESRefreshFooterNormal];
            if ([self.delegate respondsToSelector:@selector(refreshView:stateChanged:)])
            {
                [self.delegate refreshView:self stateChanged:eESRefreshStateNormal];
            }
        }else if(_state == eESRefreshStateNormal && offsetY >= validOffsetY)
        {
            //转为松开刷新
            [self.stateLable setText:dESRefreshFooterPull];
            [self setRefreshState:eESRefreshStatePull];
            if ([self.delegate respondsToSelector:@selector(refreshView:stateChanged:)])
            {
                [self.delegate refreshView:self stateChanged:eESRefreshStatePull];
            }
        }
    }else if(_state == eESRefreshStatePull)
    {
        [self.stateLable setText:dESRefreshFooterRefresh];
        //通知代理
        if ([self.delegate respondsToSelector:@selector(refreshView:stateChanged:)]) {
            [self.delegate refreshView:self stateChanged:eESRefreshStateRefresh];
        }
    }
}

@end
